<?php

/*
 +-------------------------------------------------------------------+
 |                   statistics.func.php                             |
 |                                                                   |
 | Copyright ?                          www.gde.sclab.clarkson.edu   |
 | Author: Demetrios Dimatos            www.dimatos.net              |
 | Created: August 2006                 Last modified: Oct 15, 2006  |
 +-------------------------------------------------------------------+
 | This file contains all the functions used for the statistics in   |
 | the schema browser.                                               |
 |  - /gde-1.0/templates/logged-in/browser.php                       |
 |  - /gde-1.0/includes/public/browser.ctl.php                       |
 +-------------------------------------------------------------------+
*/

//Start the SESSION vars
if (session_id() == "")
{
	session_start();
}

// Include configuration variables
 include_once('/var/www/gde-1.0/htdocs/config.php');
 // Include database connnection
 include_once(Config::base_path.Config::database_path."database.php");
  // Include templating for exceptions 
 include_once(Config::base_path.Config::adodb_path.'adodb-exceptions.inc.php');
 
 /**
  * Returns the number of records in a table
  *
  * @param string $databaseName - name of the database which to query
  * @param string $table - table needing the number of records from
  * @param string $connection_type - BACKEND or DOMAIN_SPECIFIC to determine where to do the query
  * @return integer - number for records in a table
  * @author Demetrios Dimatos
  */
 function recordCount($databaseName,$table, $connection_type)
 {
 		global $result;
 		try
 		{
 			
 			$sql = 'SELECT COUNT(*) from '.$databaseName.'.'.$table;
 			$result = doQuery($sql,$connection_type);
 		}
 		catch (Exception $e)
 		{
 			die( "ERROR executing query ".$e->getMessage());
 		}

 		if($result->RecordCount() == 0)
 		{
 			return $result = 0;
 		}
 		return  $result->fields[0];
 }
 
 /**
  * Alternative function for returning the length of a string for  UTF-8 strings that 
  * doesn't access the byte past end of the string
  *
  * @param string $str - string to determine length
  * @return integer - size of the string
  * @author Unknown
  */
 function strlen_utf8 ($str)
{
   $i = 0;
   $count = 0;
   $len = strlen ($str);
   while ($i < $len)
   {
   $chr = ord ($str[$i]);
   $count++;
   $i++;
   if ($i >= $len)
       break;

   if ($chr & 0x80)
   {
       $chr <<= 1;
       while ($chr & 0x80)
       {
       $i++;
       $chr <<= 1;
       }
   }
   }
   return $count;
}
 
 /**
  * Returns the average single record size in a table for statical purposes in bytes
  *
  * @param string $databaseName - name of database to query
  * @param string $table - name of table to query
  * @param string $connection_type - BACKEND or DOMAIN_SPECIFIC to determine where to do the query
  * @param string $number_of_records
  * @return integer - records size in bytes
  * @author Demetrios Dimatos
  */
 function recordAvgSize($databaseName,$table, $connection_type,$number_of_records)
 {
 		global $result;
 		$all_fields_arr = array();
 		$total_count =0;
 		$average_row_size =0;
 		try
 		{
 			$sql = 'SELECT * from '. $databaseName.'.'.$table;
 			$result = doQuery($sql,$connection_type);
 		}
 		catch (Exception $e)
 		{
 			die( "ERROR executing query ".$e->getMessage());
 		}
			
 		// Traverse all results and put them into an array
 		while (!$result->EOF) 
		{
			for ($i=0 ,$max=$result->FieldCount(); $i < $max; $i++)
			{
		  	 $all_fields_arr[$total_count] = $result->fields[$i];
		  	 $total_count++;
			}
			$result->MoveNext();
		}

		// Conver the array to a string 
		$all_fields_str =implode("",$all_fields_arr);
		// String size of all results is strlen + 1
		$total_size =  strlen($all_fields_str) + 1;
		// Average row/record size is $total_size/$number_of_records
		$average_row_size = $total_size/$number_of_records;
		
 		return  $average_row_size;
 }
 
/**
 * Returns teh sum of all records in a talbe in bytes
 *
 * @param string $average - average size of a record in bytes
 * @param string $number_of_records - count of records for a table
 * @return integer - sum of all records in bytes
 * @author Demetrios Dimatos
 */
 function recordSumSize($average,$number_of_records)
 {
  return  $average * $number_of_records;
 }
 
?>